package com.ry.novel.model.resp;

import com.ry.novel.enums.ResponseStatus;
import lombok.Data;

/**
 * 所有接口返回的对象
 * @author ry
 * @since 2022-01-22 19:53
 **/
@Data
public class Result<T> {
    /**
     * 响应码
     */
    private Integer code = ResultCode.OK.getCode();

    /**
     * 响应消息
     */
    private String msg = ResultCode.OK.getMsg();

    /**
     * 响应中的状态
     */
    private T data;

    private Result(){

    }

    private Result(Integer code,String msg){
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    private Result(IResultCode responseStatus){
        this.code = responseStatus.getCode();
        this.msg = responseStatus.getMsg();
    }

    private Result(T data){
        this.data = data;
    }

    /**
     * 业务处理成功无数据返回
     */
    public static Result<Void> ok(){
        return new Result<>();
    }

    /**
     * 业务处理成功，返回一个data
     */
    public static <T> Result<T> ok(T data){
        return new Result<>(data);
    }

    /**
     * 业务处理失败
     * @param responseStatus
     * @return
     */
    public static Result<Void> fail(IResultCode responseStatus){
        return new Result<>(responseStatus);
    }

    public static Result<Void> fail(Integer code,String msg){
        return new Result<>(code,msg);
    }

    public static Result<Void> fail(){
        return new Result<>();
    }

    /**
     * 系统错误
     * @return
     */
    public static Result<Void> error(){
        return new Result<>(ResultCode.ERROR);
    }
}
